package me.zhengjie.minbearwcs.corn;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.dao.stock.inf.MaterialTypeDao;
import me.zhengjie.minbearwcs.entity.PO.wcs.stock.MaterialType;
import me.zhengjie.minbearwcs.utils.ContentPool;
import me.zhengjie.minbearwcs.utils.WcsHpptUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WcsCron {

    private final WcsHpptUtil wcsHpptUtil;
    private final MaterialTypeDao materialTypeDao;




    /**
     * 定时获取物料信息
     */
    @Transactional
    @Scheduled(cron = "0 0 23 * * ? ")
    public void getMaterialTypeCron() {
        String format = LocalDateTime.now().format(ContentPool.DATE_FORMAT_YEAR_MONTH_DAY);
        List<MaterialType> materialTypes = wcsHpptUtil.fetchMaterialTypeList(format);
        log.info("wms 物料信息同步 ： {}", materialTypes);

        for (MaterialType materialType : materialTypes) {
            MaterialType materialTypeTemp = materialTypeDao.selectByMaterialCode(materialType.getMaterialCode());
            if (materialTypeTemp == null) {
                materialTypeDao.insert(materialType);
            } else {
                materialType.setId(materialTypeTemp.getId());
                materialTypeDao.update(materialType);
            }

        }
    }



}
